﻿
$c.fn.tree = function (config) {
    var obj = function (selector, config) {
        this.selector = selector;
        this.config = $.extendEx({}, this.defConfig, config);
    }
    obj.prototype.tree = function () {
        this.create();
    }
    
    obj.prototype.create = function () {
        var _obj = this;
        _obj.selector.addClass('kui-tree');
        _obj.appendNodes(_obj.selector, this.config.data);
        _obj.selector.setStyle(this.config.style);
    }
    obj.prototype.getChecked = function () {
        var _obj = this;
        var checkedList = [];
        $.each(_obj.selector.find('i'), function (i,o) {
            var cb = $(o).find('.kui-tree-checkbox');
            if (cb.hasClass(_obj.config.checkbox_checked_class)) {
                checkedList.push(o);
            }
        });
        return checkedList;
    }
    obj.prototype.appendNodes = function (elParent, children,level) {
        if (!children) {
            return;
        }
        if (!level) {
            level = 0;
        }
        var _obj = this;
        $.each(children, function (i, data) {
            var formatData =undefined;
            if (_obj.config.formatter) {
                formatData = $.extend({}, data);
                _obj.config.formatter(formatData);
            }
            var node = $('<div><i><span class="kui-tree-expander"></span><span class="kui-tree-icon"></span><span class="kui-tree-checkbox iconfont ' + _obj.config.checkbox_unchecked_class + '"></span><span class="kui-tree-title">' + (formatData?formatData.text:data.text) + '</span></i></div>').appendTo(elParent);
            node.find('i')[0].data = data;

            var isDisabled =(formatData && formatData.disabled == true) || data.disabled == true;
            if (isDisabled) {
                node.find('i').addClass('kui-disabled');
            }
            if (_obj.config.checkbox != true) {
                node.find('.kui-tree-checkbox').remove();
            }
            else {
                var cb = node.find('.kui-tree-checkbox');
                if (data.checked) {
                    cb.removeClass(_obj.config.checkbox_unchecked_class);
                    cb.addClass(_obj.config.checkbox_checked_class);
                }
                cb.click(function () {
                    if (isDisabled) {
                        return false;
                    }
                    if (cb.hasClass(_obj.config.checkbox_unchecked_class)) {
                        cb.removeClass(_obj.config.checkbox_unchecked_class);
                        cb.addClass(_obj.config.checkbox_checked_class);
                    }
                    else {
                        cb.removeClass(_obj.config.checkbox_checked_class);
                        cb.addClass(_obj.config.checkbox_unchecked_class);
                    }
                    return false;//使click事件不冒泡.
                });
            }
            //alert(level);
            for (var identNum = 0; identNum < level; identNum++) {
                node.find('i').prepend('<span class="kui-tree-ident"></span>');
            }
            if (data.children && data.children.length > 0) {
                node.addClass('kui-tree-parent');
                if (data.closed == true) {
                    node.addClass('kui-tree-closed');
                }
                else {
                    node.addClass('kui-tree-open');
                }
                node.find('i').click(function () {
                    //alert($(this).length)
                    $(this).parent().toggleClass('kui-tree-closed');
                    $(this).parent().toggleClass('kui-tree-open');
                    if (_obj.config.onClick) {
                        _obj.config.onClick(node, data);
                    }
                    return false;//事件不冒泡,否则点子元素,父元素也会发生click事件.
                });
                _obj.appendNodes(node, data.children, level+1);//注:level+1不改变level本身的值,否则level一直往上加就不对了.

            }
            else {
                node.addClass('kui-tree-leaf');
                //node.find('.kui-tree-expander').remove();
                node.find('i').click(function () {
                    if (_obj.config.onClick) {
                        _obj.config.onClick(node, data);
                    }
                    return false;//事件不冒泡,否则点子元素,父元素也会发生click事件.
                });
            }
        });
    }
    obj.prototype.defConfig = {
        checkbox_unchecked_class: 'icon-checkbox-unchecked',
        checkbox_checked_class: 'icon-checkbox-checked1'
    }

    var _obj = new obj($(this.jo), config || {});
    _obj.tree();
    return _obj;
};